using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

namespace Coffee.SurveyControls.SurveyQuestions
{


    /// <summary>
    /// Summary description for SQTextBox
    /// </summary>
    /// 

    public class SQTextBox : SurveyQuestionRow
    {
        bool bReadonly = false;
        string nullValue = string.Empty;

        public SQTextBox(SurveyControl parent, XmlNode configuration)
            : base(parent, configuration)
        {
        }

        public override bool Validate()
        {
            return true;
        }

        public override void LoadData()
        {
            if (Parent.Mapper != null)
            {
                ((Label)previousAnswerControl).Text = HttpUtility.HtmlEncode(Parent.Mapper.Previous.GetProperty(ctlId));

                object oCurrValue = Parent.Mapper.Current.GetProperty(ctlId);
                string currvalue = null;

                if(oCurrValue!=null)
                    currvalue = oCurrValue.ToString();

                if(nullValue.Length > 0)
                {
                    if(currvalue==null) currvalue = nullValue;
                }

                if (bReadonly)
                {
                    ((Label)currentAnswerControl).Text = currvalue;
                }
                else
                {
                    ((TextBox)currentAnswerControl).Text = currvalue;
                }
            }

            //((TextBox)currentYearData).Enabled = !locked;
        }

        public override void SaveData()
        {
            XmlNode dataType;

            if (!bReadonly)
            {
                TextBox tCurr = (TextBox)CurrentControl;

                if (Parent.Mapper != null)
                {
                    SQBindingContext binder = new SQBindingContext(rootNode);

                    binder.WriteProperties(Parent.Mapper, tCurr.Text);
                }
            }
            
        }

        protected override Control CreatePreviousDataControl()
        {
            Label ctl = new Label();
            ctl.CssClass = "surveytable-text";
            return ctl;
        }

        protected override Control CreateCurrentDataControl()
        {
            Control ctl;

            if (!bReadonly)
            {
                ctl = new TextBox();
            }
            else
            {
                ctl = new Label();
            }

            XmlNode dataType = null;

            if ((dataType = rootNode.Attributes["datatype"]) != null)
            {
                if (
                    (dataType.Value == "integer") ||
                    (dataType.Value == "decimal")
                    )
                {
                    if (!bReadonly)
                    {
                        Parent.Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "ConstrainNumeric", "<script>function ConstrainNumeric(){if((event.keyCode == 189 || event.keyCode == 109)||(event.keyCode >= 48 && event.keyCode <= 57)||(event.keyCode == 44)){return true;}else{return false;}}</script>");
                        ((TextBox)ctl).Attributes["onkeypress"] = "return ConstrainNumeric();";
                    }
                }
            }

            if (!bReadonly)
            {
                ((TextBox)ctl).CssClass = "surveytable-text surveytable-entry-border surveytable-textbox";
            }
            else
            {
                ((Label)ctl).CssClass = "surveytable-text";
            }

            return ctl;
        }

        protected override void Initialize()
        {

        }
    }
}